<?php
class ModelMenuMenu extends Model {
	public function addMenu($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "menu SET restaurant_id = '" . (int)$this->restaurant->getRestaurantId() . "', parent_id = '" . (int)$data['parent_id'] . "', `top` = '" . (isset($data['top']) ? (int)$data['top'] : 0) . "', `column` = '" . (int)$data['column'] . "', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "', date_added = NOW()");

		$menu_id = $this->db->getLastId();

		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "menu SET image = '" . $this->db->escape($data['image']) . "' WHERE menu_id = '" . (int)$menu_id . "'");
		}

		foreach ($data['menu_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "menu_description SET menu_id = '" . (int)$menu_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}

		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'menu_id=" . (int)$menu_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}

		$this->cache->delete('menu_menu');
	}

	public function editMenu($menu_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "menu SET restaurant_id = '" . (int)$this->restaurant->getRestaurantId() . "', parent_id = '" . (int)$data['parent_id'] . "', `top` = '" . (isset($data['top']) ? (int)$data['top'] : 0) . "', `column` = '" . (int)$data['column'] . "', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "', date_modified = NOW() WHERE menu_id = '" . (int)$menu_id . "'");

		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "menu SET image = '" . $this->db->escape($data['image']) . "' WHERE menu_id = '" . (int)$menu_id . "'");
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "menu_description WHERE menu_id = '" . (int)$menu_id . "'");

		foreach ($data['menu_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "menu_description SET menu_id = '" . (int)$menu_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'menu_id=" . (int)$menu_id. "'");

		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'menu_id=" . (int)$menu_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}

		$this->cache->delete('menu_menu');
	}

	public function deleteMenu($menu_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "menu WHERE menu_id = '" . (int)$menu_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "menu_description WHERE menu_id = '" . (int)$menu_id . "'");
		$this->cache->delete('menu_menu');
	}

	public function getMenu($menu_id) {
		$sql = "SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'menu_id=" . (int)$menu_id . "') AS keyword FROM " . DB_PREFIX . "menu p LEFT JOIN " . DB_PREFIX . "menu_description md ON (p.menu_id = md.menu_id) WHERE p.menu_id = '" . (int)$menu_id . "' AND md.language_id = '" . (int)$this->config->get('config_language_id') . "'";

		$query = $this->db->query($sql);

		return $query->row;
	}

	public function getMenusByParentId($parent_id = 0){
		$cache_key = 'menu_menu.' . (int)$this->config->get('config_language_id') . '.' . $parent_id . '.' . $this->restaurant->getRestaurantId();
		$menu_data = $this->cache->get($cache_key);

		if (!$menu_data) {
			$sql   = "SELECT DISTINCT m.*, md.name, md.description";
			$sql  .= " FROM (" . DB_PREFIX . "menu m LEFT JOIN " . DB_PREFIX . "menu_description md ON (m.menu_id = md.menu_id))";
				
			$sql .= "  WHERE md.language_id = '" . (int)$this->config->get('config_language_id') . "'";

			$sql .= " AND LCASE(m.restaurant_id) = '" . (int)$this->restaurant->getRestaurantId() . "'";
			
			$sql .= " AND LCASE(m.parent_id) = '" . (int)$parent_id . "'";
			
			$sql .= " ORDER BY m.sort_order, md.name, m.parent_id";
			
			$sql .= " ASC";

			$query = $this->db->query($sql);
			
			$menu_data = array();
		
			foreach ($query->rows as $result) {
				$menu_data[] = array(
					'menu_id' => $result['menu_id'],
					'name'        => $this->getPath($result['menu_id'], $this->config->get('config_language_id')),
					'status'  	  => $result['status'],
					'sort_order'  => $result['sort_order']
				);
			
				$menu_data = array_merge($menu_data, $this->getMenusByParentId($result['menu_id']));
			}
			
			$this->cache->set($cache_key, $menu_data);
		}
		
		return $menu_data;
	}

	public function getMenus($data = array()) {
		if($data) {
			$sql   = "SELECT DISTINCT m.*, md.name, md.description";
			$sql  .= " FROM (" . DB_PREFIX . "menu m LEFT JOIN " . DB_PREFIX . "menu_description md ON (m.menu_id = md.menu_id))";
				
			$sql .= "  WHERE md.language_id = '" . (int)$this->config->get('config_language_id') . "'";
				
			if (!empty($data['filter_name'])) {
				$sql .= " AND LCASE(md.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
			}
				
			if(!empty($data['filter_parent_menu'])) {
				$sql .= " AND LCASE(m.parent_id) = '" . (int)$data['filter_parent_menu'] . "'";
			}
			
			if(!empty($data['parent_id'])) {
				$sql .= " AND LCASE(m.parent_id) = '" . (int)$data['parent_id'] . "'";
			}
			
			$sql .= " AND LCASE(m.restaurant_id) = '" . (int)$this->restaurant->getRestaurantId() . "'";
				
			if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
				$sql .= " AND m.status = '" . (int)$data['filter_status'] . "'";
			}
				
			$sort_data = array(
				'md.name',
				'm.sort_order',
				'm.status'
			);
					
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];
			} else {
				$sql .= " ORDER BY m.sort_order, md.name, m.parent_id";
			}
				
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
	
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}
	
				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}
	
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}
	
			$query = $this->db->query($sql);
	
			$menu_data = $query->rows;
	
			return $menu_data;
		} else {
			$key = 'menu_menu.' . (int)$this->config->get('config_language_id') . '.' . $this->restaurant->getRestaurantId();
			$menu_data = $this->cache->get($key);
				
			if (!$menu_data) {
				$sql   = "SELECT DISTINCT m.*, md.name, md.description";
				$sql  .= " FROM (" . DB_PREFIX . "menu m LEFT JOIN " . DB_PREFIX . "menu_description md ON (m.menu_id = md.menu_id))";
					
				$sql .= "  WHERE md.language_id = '" . (int)$this->config->get('config_language_id') . "'";
					
				$query = $this->db->query($sql);
					
				$menu_data = $query->rows;
					
				$this->cache->set($key, $menu_data);
			}
				
			return $menu_data;
		}
	}

	public function getTotalMenus($data = array()) {
		$sql  = "SELECT COUNT(DISTINCT m.menu_id) AS total";
		$sql  .= " FROM (" . DB_PREFIX . "menu m LEFT JOIN " . DB_PREFIX . "menu_description md ON (m.menu_id = md.menu_id))";
			
		$sql .= "  WHERE md.language_id = '" . (int)$this->config->get('config_language_id') . "'";
			
		if (!empty($data['filter_name'])) {
			$sql .= " AND LCASE(md.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
		}

		if(!empty($data['filter_parent_menu'])) {
			$sql .= " AND LCASE(m.parent_id) = '" . (int)$data['filter_parent_menu'] . "'";
		}

		if(!empty($data['parent_id'])) {
			$sql .= " AND LCASE(m.parent_id) = '" . (int)$data['parent_id'] . "'";
		}

		$sql .= " AND LCASE(m.restaurant_id) = '" . (int)$this->restaurant->getRestaurantId() . "'";
			
		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$sql .= " AND m.status = '" . (int)$data['filter_status'] . "'";
		}

		$query = $this->db->query($sql);

		return $query->row['total'];
	}


	public function getPath($menu_id) {
		$query = $this->db->query("SELECT name, parent_id FROM " . DB_PREFIX . "menu m LEFT JOIN " . DB_PREFIX . "menu_description md ON (m.menu_id = md.menu_id) WHERE m.menu_id = '" . (int)$menu_id . "' AND md.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY m.sort_order, md.name ASC");

		if ($query->row['parent_id']) {
			return $this->getPath($query->row['parent_id'], $this->config->get('config_language_id')) . $this->language->get('text_separator') . $query->row['name'];
		} else {
			return $query->row['name'];
		}
	}

	/**
	 *
	 * Check if given $ancestor_menu_id is ancestor of given $menu_id or not
	 * @param unknown_type $menu_id
	 * @param unknown_type $ancestor_menu_id
	 */
	public function isAncestor($menu_id, $ancestor_menu_id) {
		$query = $this->db->query("SELECT m.menu_id, parent_id FROM " . DB_PREFIX . "menu m LEFT JOIN " . DB_PREFIX . "menu_description md ON (m.menu_id = md.menu_id) WHERE m.menu_id = '" . (int)$menu_id . "' AND md.language_id = '" . (int)$this->config->get('config_language_id') . "'");

		if ($query->row['parent_id']) {
			if($query->row['parent_id'] == $ancestor_menu_id) {
				return true;
			}
				
			return $this->isAncestor($query->row['parent_id'], $ancestor_menu_id);
		} else {
			return false;
		}
	}

	/**
	 *
	 * Check if given $descendant_menu_id is descendant of given $menu_id or not
	 * @param unknown_type $menu_id
	 * @param unknown_type $descendant_menu_id
	 */
	public function isDescendant($menu_id, $descendant_menu_id) {
		$query = $this->db->query("SELECT m.menu_id, name, parent_id FROM " . DB_PREFIX . "menu m LEFT JOIN " . DB_PREFIX . "menu_description md ON (m.menu_id = md.menu_id) WHERE m.parent_id = '" . (int)$menu_id . "' AND md.language_id = '" . (int)$this->config->get('config_language_id') . "'");

		if ($query->rows) {
			foreach ($query->rows as $result) {
				if($result['menu_id'] == $descendant_menu_id) {
					return true;
				}
			}
			foreach ($query->rows as $result) {
				if($result['menu_id'] != $descendant_menu_id) {
					return $this->isDescendant($result['menu_id'], $descendant_menu_id);
				}
			}
		}

		return false;
	}

	/**
	 * Check if given menu has decendant or not
	 * Enter description here ...
	 * @param unknown_type $menu_id
	 */
	public function hasDecendant($menu_id) {
		$data = array(
			'parent_id'	=>	$menu_id
		);

		if($this->getTotalMenus($data) > 0){
			return true;
		} else {
				
		}

		return false;
	}

	public function getMenuDescriptions($menu_id) {
		$menu_description_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "menu_description WHERE menu_id = '" . (int)$menu_id . "'");

		foreach ($query->rows as $result) {
			$menu_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'meta_keyword'     => $result['meta_keyword'],
				'meta_description' => $result['meta_description'],
				'description'      => $result['description']
			);
		}

		return $menu_description_data;
	}

}
?>